package com.itcast.controller;

import com.itcast.dao.ShoppingDao;
import com.itcast.domain.Shopping;
import com.itcast.domain.Shopping_Order;
import com.itcast.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.util.*;

@Controller
@CrossOrigin
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private ShoppingDao shoppingDao;
    @Autowired
    private OrderService orderService;

    /**
     * 结算购物车，形成订单
     * @return
     */
    @RequestMapping(value = "/settle", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String settle() throws Exception {
        //先查询购物车中的商品，把购物车商品形成订单
        List<Shopping> list = shoppingDao.findAll();
        return orderService.settle(list);
    }

    /**
     * 删除单个订单
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/byTimeDelete/{time}", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String byTimeDelete(@PathVariable("time") String time) throws Exception {
        return orderService.byTimeDelete(time);
    }

    /**
     * 查询订单时间
     * @return
     */
    @RequestMapping(value = "/findTime", produces = "application/json;charset=utf-8")
    @ResponseBody
    public TreeSet<Date> findTime() {
        return orderService.findTime();
    }

    /**
     * 通过订单时间查询某个时间下的订单
     * @return
     */
    @RequestMapping(value = "/byTimeFind/{time}", produces = "application/json;charset=utf-8")
    @ResponseBody
    public List<Shopping_Order> byTimeFind(@PathVariable("time") String time) throws ParseException {
        return orderService.byTimeFind(time);
    }


}
